
export default {
  flattern (model) {
    if (model.userData.mode === "2d") {
      return;
    }
    let scaleZ = model.scale.z;
    model.userData.originalScale = scaleZ;
    model.userData.mode = "2d";
    let height = model.modelSize.z;
    let minHeight = 1;
    let threshold = (minHeight * scaleZ) / height;
    let step = (scaleZ - threshold) / 20;
    let frame = null;
    function animate () {
      model.scale.z -= step;
      if (model.scale.z <= threshold) {
        cancelAnimationFrame(frame);
      } else {
        frame = requestAnimationFrame(animate);
      }
    }
    animate();
  },
  extrust (model) {
    if (model.userData.mode === "3d") {
      return;
    }
    let scaleZ = model.scale.z;
    let originalScale = model.userData.originalScale;
    model.userData.mode = "3d";
    let step = (originalScale - scaleZ) / 20;
    let frame = null;
    function animate () {
      let newScale = model.scale.z + step;
      if (newScale > originalScale) {
        model.scale.z = originalScale;
        cancelAnimationFrame(frame);
      } else {
        model.scale.z = newScale;
        frame = requestAnimationFrame(animate);
      }
    }
    animate();
  },

  flatternAll () {
    tb.world.children.forEach(model => {
      const list = ['地面', '标签']
      if (list.includes(model.name)) {
        return;
      }
      try {
        this.flattern(model);
        model.removeTooltip();
        if (model.boundingBox) {
          model.boundingBox.visible = false;
        }
      } catch (err) {
        console.log(model.name);
        console.error(err);
      }
    });
  },
  flatternOther (selName) {

    const list = ['10栋', '13栋', '14栋', '15栋', '21栋']
    tb.world.children.forEach(model => {
      const name = model.name.split('-')[0]
      if (list.includes(name)&& name===selName) {
        return;
      }
      try {
        this.flattern(model);
        model.removeTooltip();
        if (model.boundingBox) {
          model.boundingBox.visible = false;
        }
      } catch (err) {
        console.log(model.name);
        console.error(err);
      }
    });
  },
  extrustAll () {
    const list = ['21栋']
    tb.world.children.forEach(model => {
      const name = model.name.split('-')[0]
      if (list.includes(name)) {
        return;
      }
      this.extrust(model);
    });
  },
  extrustOther (selName) {
    const list = ['10栋', '13栋', '14栋', '15栋', '21栋']
    tb.world.children.forEach(model => {
      const name = model.name.split('-')[0]
      if (list.includes(name)&& name===selName) {
        return;
      }
      this.extrust(model);
    });
  },
  css (el, css) {
    for (let key in css) {
      let k = key.replace(/([A-Z])/g, "-$1").toLowerCase();
      el.style[k] = css[key];
    }
  },
  max2lnglat (maxCoord, origin) {
    // 3dmax坐标转经纬度
    let mercatorOrigin = mapboxgl.MercatorCoordinate.fromLngLat(origin);
    let xdiff = maxCoord.x * mercatorOrigin.meterInMercatorCoordinateUnits();
    let ydiff = maxCoord.y * mercatorOrigin.meterInMercatorCoordinateUnits();
    let newMercator = new mapboxgl.MercatorCoordinate(
      mercatorOrigin.x - xdiff,
      mercatorOrigin.y + ydiff
    );
    return newMercator.toLngLat();
  },
  extend (dest, ...sources) {
    for (const src of sources) {
      for (const k in src) {
        dest[k] = src[k]
      }
    }
    return dest
  },
  endsWith (string, suffix) {
    return string.indexOf(suffix, string.length - suffix.length) !== -1
  },
  debounce (func, wait) {
    let timer
    return function () {
      const context = this
      const args = arguments

      if (timer) clearTimeout(timer)

      const callNow = !timer

      timer = setTimeout(() => {
        timer = null
      }, wait)

      if (callNow) func.apply(context, args)
    }
  },
  formatTime (fmt, date) {
    let now = new Date()
    if (date) {
      now = date
    }
    let weekday = [
      '星期日',
      '星期一',
      '星期二',
      '星期三',
      '星期四',
      '星期五',
      '星期六'
    ]
    var o = {
      'M+': now.getMonth() + 1, //月份
      'd+': now.getDate(), //日
      'H+': now.getHours(), //小时
      'm+': now.getMinutes(), //分
      's+': now.getSeconds(), //秒
      'q+': Math.floor((now.getMonth() + 3) / 3), //季度
      S: ('000' + now.getMilliseconds()).substr(
        ('' + now.getMilliseconds()).length
      ), //毫秒
      W: weekday[now.getDay()]
    }
    if (/(y+)/.test(fmt))
      fmt = fmt.replace(
        RegExp.$1,
        (now.getFullYear() + '').substr(4 - RegExp.$1.length)
      )
    for (var k in o)
      if (new RegExp('(' + k + ')').test(fmt))
        fmt = fmt.replace(
          RegExp.$1,
          RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)
        )
    return fmt
  },
  deg2arc (angle) {
    return angle / 180 * Math.PI
  }
};
